package com.sj.purchase.modules.cost.config.task;

import com.sj.purchase.modules.cost.domain.WarehouseEnum;
import com.sj.purchase.modules.cost.repository.stock.SkuStockRepository;
import com.sj.purchase.modules.cost.service.order.OutOrderItemCostService;
import com.sj.purchase.modules.cost.service.order.OutOrderService;
import com.sj.purchase.modules.cost.service.stock.SkuStockReportService;
import com.sj.purchase.modules.cost.service.stock.StockReportService;
import com.sj.purchase.modules.cost.views.order.OutOrderVO;
import com.sj.purchase.modules.cost.views.out_order.OutOrderCostVO;
import com.sj.purchase.modules.cost.views.stock.BeginStockVO;
import com.sj.purchase.modules.cost.views.stock.EndStockVO;
import com.sj.purchase.modules.cost.views.stock.SkuStockReportVO;
import com.sj.purchase.util.DateUtils;
import com.sj.purchase.util.QiNiuUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Create By MaJinGe on 2017-12-6
 */
@Component
public class UploadExcel {

    @Autowired
    private OutOrderItemCostService outOrderItemCostService;

    @Autowired
    private SkuStockReportService skuStockReportService;

    @Autowired
    private SkuStockRepository skuStockRepository;

    @Autowired
    private OutOrderService outOrderService;

    @Autowired
    private StockReportService stockReportService;

    private Date startDay = new Date();

    private Date endDay = new Date();

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public void setDay(Date startDay) {
        try {
            this.startDay = simpleDateFormat.parse(simpleDateFormat.format(startDay));
            this.endDay = simpleDateFormat.parse(simpleDateFormat.format(DateUtils.addDays(startDay, 1)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }


    /**
     * 出库单成本明细
     */
    public void outOrder() {
        List<OutOrderCostVO> outOrderCostVOList = new ArrayList<>();
        outOrderCostVOList.addAll(outOrderItemCostService.search(null, startDay, endDay, null).getContent());
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), OutOrderCostVO.class, outOrderCostVOList);
        this.uploadExcel(workbook, "outOrderItem/", "订单详情.xls");
    }

    /**
     * sku出入库查询
     */
    public void skuStock() {
        WarehouseEnum[] warehouseEnum = WarehouseEnum.values();
        for (WarehouseEnum warehouseEnum1 : warehouseEnum) {
            if (warehouseEnum1.getName() == "济南主仓") {
                List<SkuStockReportVO> skuStockReportVOList = new ArrayList<>();
                List<String> skuNoList = skuStockRepository.findBaseStockSku(startDay, endDay, warehouseEnum1.name(), null).getContent();
                skuStockReportVOList.addAll(skuStockReportService.search(warehouseEnum1, startDay, endDay, skuNoList));
                Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), SkuStockReportVO.class, skuStockReportVOList);
                this.uploadExcel(workbook, "skuStock/", warehouseEnum1.getName() + "进销库存报表.xls");
            }
        }
    }

    /**
     * 订单利润
     */
    public void orderCost(){
        List<OutOrderVO> outOrderVOList = new ArrayList<>();
        outOrderVOList.addAll(outOrderService.findAll(startDay,endDay,null,null).getContent());
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), OutOrderVO.class, outOrderVOList);
        this.uploadExcel(workbook,"orderCostPrice/","订单利润.xls");
    }

    /**
     * 进销存报表-->期初
     */
    public void beginStockReport() {
        List<BeginStockVO> beginStockVOList = new ArrayList<>();
        beginStockVOList.addAll(stockReportService.getBeginStockContentAndTotal(DateUtils.getStartOfDay(startDay), "", null).getLeft());
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), BeginStockVO.class, beginStockVOList);
        this.uploadExcel(workbook, "beginReportForm/", "期初进销存报表.xls");
    }

    /**
     * 进销存报表-->期末
     */
    public void endStockReport() {
        List<EndStockVO> endStockVOList = new ArrayList<>();
        endStockVOList.addAll(stockReportService.getEndStockContentAndTotal(startDay, "", null).getLeft());
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), EndStockVO.class, endStockVOList);
        this.uploadExcel(workbook, "endReportForm/", "期末进销存报表.xls");
    }

    /**
     * 上传文件
     */
    private void uploadExcel(Workbook workbook, String prefix, String suffix) {
        String day = simpleDateFormat.format(startDay);
        String fileName = prefix + day + suffix;
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            workbook.write(out);
            ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
            QiNiuUtil.uploadByByte(in, fileName);
            in.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
